# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

from goodDesignImage2018Spider.items import GooddesignimagespiderItem
from goodDesignImage2018Spider.items import WaiMaiYuanItem
from goodDesignImage2018Spider.caipuItems import *
from goodDesignImage2018Spider.spiders.goodDesignImg2017Spider2 import Gooddesignimg2017spiderSpider2
from goodDesignImage2018Spider.spiders.meishijSpider import MeishijSpider
from goodDesignImage2018Spider.spiders.meishijDetailSpider import MeishijDetailSpider
from goodDesignImage2018Spider.util.Logger import Logger
from goodDesignImage2018Spider.util.mysql_opt import mysql_opt
from goodDesignImage2018Spider.util import my_util
import json
from goodDesignImage2018Spider.dto.waimaiyuanDtos import *
from goodDesignImage2018Spider.dao.waimaiyuan.caipuInfoDao import *
from goodDesignImage2018Spider.dao.waimaiyuan.fileInfoDao import *
from goodDesignImage2018Spider.dao.waimaiyuan.caipuStepInfoDao import *


class GooddesignimagespiderPipeline:
    def __init__(self):
        self.logger = Logger()
        self.conn, self.cursor = mysql_opt.get_conn()

    def process_item(self, item, spider):
        if isinstance(spider, Gooddesignimg2017spiderSpider2):
            if isinstance(item, GooddesignimagespiderItem):
                # 重复则不入库了
                old_data = self.check_had_data(item['award_id'], item['img_download_url'])
                if old_data:
                    self.logger.error(msg="重复插入=" + str(old_data))
                    return item
                item_values = tuple(item.values())
                insert_sql = 'INSERT INTO `robot_good_design_img_record` ( `award_id`, `award_code`, `detail_url`,`year`, `download_status`, `award_type`,`img_download_url`, `img_file_name`)' \
                             + 'VALUES (%s,%s,%s,%s,%s,%s,%s,%s);'
                self.cursor.execute(insert_sql, item_values)
                self.conn.commit()
                self.logger.error(msg="入库一个图片" + str(item))

        if isinstance(spider, MeishijSpider):
            if isinstance(item, WaiMaiYuanItem):
                item_values = tuple(item.values())
                insert_sql = 'INSERT INTO `waimaiyuan_item` ( `item_name`, `item_parent_id`, `item_remark`,`item_biz_type`)' \
                             + 'VALUES (%s,%s,%s,%s);'
                self.cursor.execute(insert_sql, item_values)
                self.conn.commit()
                self.logger.error(msg="入库一个分类" + str(item))

        if isinstance(spider, MeishijDetailSpider):
            if isinstance(item, CaipuInfoItem2):
                cai_pu_info_dto = item['caipu_info_dto']
                print(cai_pu_info_dto.caipu_title)
                # 插入菜谱主表
                cp_info_id = self.insert_cp_main_info(cai_pu_info_dto)
                print(cp_info_id)

                # 插入菜谱主图
                main_img_id = self.insert_cp_main_img(cai_pu_info_dto, cp_info_id)
                print(main_img_id)

                # 插入步骤和步骤图
                self.insert_step_and_img_info(cai_pu_info_dto, cp_info_id)

        return item

    def insert_cp_main_img(self, cai_pu_info_dto, cp_info_id):
        main_img = cai_pu_info_dto.caipu_imgs
        file_name, suffix = my_util.get_file_name_from_url(main_img.file_third_network_url)
        main_img_info = FileInfo(file_third_network_url=main_img.file_third_network_url,
                                 file_biz_type=0,
                                 file_name=file_name,
                                 file_suffix=suffix,
                                 file_remark=main_img.file_remark,
                                 file_biz_id=cp_info_id,
                                 )
        main_img_id = FileInfo.add_file_info(main_img_info)
        return main_img_id

    def insert_cp_main_info(self, cai_pu_info_dto):
        cp_info_dao = CaipuInfo(caipu_title=cai_pu_info_dto.caipu_title,
                                caipu_third_source_id=cai_pu_info_dto.caipu_third_source_id,
                                caipu_main_ingredients_json=cai_pu_info_dto.caipu_main_ingredients_json,
                                caipu_others_ingredients_json=cai_pu_info_dto.caipu_others_ingredients_json,
                                caipu_intro=cai_pu_info_dto.caipu_intro,
                                caipu_tips=cai_pu_info_dto.caipu_tips,
                                caipu_tech=cai_pu_info_dto.caipu_tech,
                                caipu_category_id_1=cai_pu_info_dto.caipu_category_id_1,
                                caipu_category_id_2=cai_pu_info_dto.caipu_category_id_2,
                                )
        cp_info_id = CaipuInfo.add_cp_info(cp_info_dao)
        return cp_info_id

    def check_had_data(self, award_id, img_download_url):
        query_map = (award_id, img_download_url)
        query_sql = 'SELECT * FROM `robot_good_design_img_record` where award_id=%s and img_download_url=%s limit 1;'
        self.cursor.execute(query_sql, query_map)
        return self.cursor.fetchone()

    def insert_step_and_img_info(self, cai_pu_info_dto, caipu_info_id):
        steps = cai_pu_info_dto.caipu_steps
        for step in steps:
            # 步骤信息
            caipu_step_dao = CaipuStepInfo(
                caipu_info_id=caipu_info_id,
                caipu_step_sort=step.caipu_step_sort,
                caipu_step_text=step.caipu_step_text,
            )

            # 步骤图
            step_img = step.step_img
            step_img_info = None
            if step_img is not None:
                file_name, suffix = my_util.get_file_name_from_url(step_img.file_third_network_url)
                step_img_info = FileInfo(file_third_network_url=step_img.file_third_network_url,
                                         file_biz_type=1,
                                         file_name=file_name,
                                         file_suffix=suffix,
                                         file_remark=step_img.file_remark
                                         )
            CaipuStepInfo.add_step_and_img_info(caipu_step_dao, step_img_info)
